/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.Event;
import entity.Invitation;
import entity.User;
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.EJB;

@Stateless
public class EventManager {
	
    @PersistenceContext
    public EntityManager em;
    
    @EJB
    InvitationManager invitationManager;

    public List<Event> getAllEvents() {
        return em.createNamedQuery(Event.FIND_ALL, Event.class)
                .getResultList(); 
    }

    public List<Event> getAllEventsOrganized(User user) {
        return em.createNamedQuery(Event.FIND_ALL_EVENT_ORGANIZED, Event.class)
                .setParameter("organizer", user).getResultList();
    }

    public List<Event> getAllEventsTakePart(User user) {
        List<Event> eventi = new ArrayList<Event>();
        List<Invitation> invitiUser= em.createNamedQuery(Invitation.INVITATIONS_BY_USER, Invitation.class)
                .setParameter("user", user).getResultList(); 
        if(invitiUser != null && invitiUser.isEmpty()==false){
            int i = 0;
            while( i < invitiUser.size() )
            {
                if( invitiUser.get( i ).getAnswer() == 1)
                    eventi.add(invitiUser.get(i).getEvent());
                i++;
            }
        }
        return eventi;        
    }

    public List<Event> getAllEventsDeclined(User user) {
        
        List<Event> eventiDeclinati=  new ArrayList<Event>();
        List<Invitation> invitiUser= em.createNamedQuery(Invitation.INVITATIONS_BY_USER, Invitation.class)
                .setParameter("user", user).getResultList();
        if(invitiUser.isEmpty()==false){
            for(Invitation i: invitiUser){
                if(i.getAnswer()== 2)
                    eventiDeclinati.add(i.getEvent());
            }
        }
        return eventiDeclinati;        
    
        
    }
   /* 
     public List<Event> getAllEventsIvitationUser(User user) {
        List<Event> eventi = new ArrayList<Event>();
        List<Invitation> invitiUser= em.createNamedQuery(Invitation.INVITATIONS_BY_USER, Invitation.class)
                .setParameter("user", user).getResultList(); 
        for(Invitation i: invitiUser){
            eventi.add(i.getEvent());
        }
        return eventi;        
    
    }
*/
    public void addEvent(Event eventBean) {
        try{
            System.out.println("Sto cercando di aggiungere l'evento: "+eventBean.getName());
            em.persist(eventBean);
        }
        catch( EntityExistsException e ){      
        }  
        
    }
    public void editEvent(Event event,Event oldEvent){
        System.out.println("Sto cercando di modificare l'evento: "+event.getName());
        System.out.println("Sto cercando di modificare l'evento con id"+oldEvent.getId());
        Event evento = (Event) em.find(Event.class, oldEvent.getId());
        if (evento != null) {
            Event eventoNuovo = new Event();
            eventoNuovo= em.merge(evento);
            eventoNuovo.setDateStart(event.getDateStart());
            eventoNuovo.setDateEnd(event.getDateEnd());
            eventoNuovo.setDescription(event.getDescription());
            eventoNuovo.setFirstDate(event.getFirstDate());
            eventoNuovo.setName(event.getName());
            eventoNuovo.setOrganizer(event.getOrganizer());
            eventoNuovo.setPlace(event.getPlace());
            eventoNuovo.setTypeIo(event.getTypeIo());
            eventoNuovo.setTypePp(event.getTypePp());
             System.out.println("evento modificato ccon id"+eventoNuovo.getId());
           
        } else
            System.out.println("Evento non trovato nel db");
        
    }

    public boolean cancelEventDb(Event event){
        Event ac = em.getReference(Event.class, event.getId());
        
        
        if (ac != null)
        {
            invitationManager.cancelInvitations(ac.getInvitationCollection());

            em.remove(ac);
            return true;
        }
        System.out.println("volevo rimuovere un evento");
        return false;
    }
    

}
